$(document).ready(function(){
    
    $('#submit').click(function(){
        
        if($('#username').val() == ''){
            layer.msg('用户名为空', {
                time: 1000
            });
            return 
        }

        if($('#password').val() == ''){
            layer.msg('密码为空', {
                time: 1000
            });
            return false;
        }

        if($('#email').val() == ''){
            layer.msg('邮箱为空', {
                time: 1000
            });
            return false;
        }

        if($('#verCodeText').val() == ''){
            layer.msg('验证码为空', {
                time: 1000
            });
            return false;
        }

        var reg = /^\w{3,16}$/;
        if(!reg.test(parseInt($('#username').val()))){
            layer.msg('用户名格式错误，由字母，数字，下划线组成，长度为3到16位',{
                icon: 2,
                timer:1500
            });
            return false;
        }

        var reg = /^[0-9A-Za-z]{6,18}$/;
        if(!reg.test($('#password').val())){
            layer.msg('密码格式错误，应为数字和字母组成，长度为6-18',{
                icon: 2,
                timer:1500
            });
            return false;
        }
        if($('#password').val() !== $('#comfirePwd').val()){
            layer.msg('两次密码不一致!',{
                icon: 2,
                timer:1500
            });
            return false;
        }

        var reg = /^[A-Za-z0-9]\w{5,17}@(vip\.(126|163|188)\.com|163\.com|126\.com|yeach\.net)/;
        if(!reg.test($('#email').val())){
            layer.msg('邮箱格式错误，目前只能由网易邮箱注册！',{
                icon: 2,
                timer:1500
            });
            return false;
        }

        if($('#verCodeText').val() !== $('#verCodeText').next().text()){
            layer.msg('验证错误！',{
                icon: 2,
                timer:1500
            });
            return false;
        }

        var obj ={
            username:$('#username').val(),
            password:$("#password").val(),
            email:$("#email").val()
        }

        $.ajax({
            type: "post",  //默认get
            url: "./php/register.php",  //默认当前页
            data: obj,  //格式{key:value}
            dataType: "json",
            success: function (response) {  //请求成功回调
                var {status,meta:{msg,data}} = response;
                if(status == 0){
                    layer.msg(msg,{
                        icon: 2,
                        time:1500
                    });
                }if(status == 1){
                    layer.msg(msg,{
                        icon: 1,
                        time:1500
                    },function(){
                        layer.confirm('是否登录', {
                            btn: ['是','否'] //按钮
                        }, function(){
                            window.location.href = 'login.html';
                        }, function(){
                            layer.msg('也可以这样', {
                                time: 1500, 
                                btn: ['明白了', '知道了']
                            });
                        });
                    });
                }
            }
        });

        $('#username').val(' ');
        $('#password').val(' ');
        $('#comfirePwd').val(' ');
        $('#email').val(' ');
        $('#verCodeText').val(' ');
    });


    $('#verCodeText').next().text(rand16());
    $('#verCodeText').next().click(function(){
        $(this).text('');
        $(this).text(rand16());
    });
    function rand16(){
        var str = '';
        var arr =[4, 6]
        var length = parseInt(Math.random()*3);
        for(var i=0; i<arr[length]; i++){
            str += parseInt(Math.random()*16).toString(16)
        }
        return str;
    }
});